package com.yinhe.music.yhmusic.instrument;

import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.SearchInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

import java.util.ArrayList;
import java.util.List;

public class InstrumentListPresenter extends BasePresenter<BaseModel, InstrumentListContract.IInstrumentListView> implements
        InstrumentListContract.IInstrumentListPresenter {

    @Override
    public void getInstrumentList() {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getInstrumentList()
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(instrumentBean -> {
                    getView().hideLoading();
                    parseInstrumentBean(instrumentBean);
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void getSearchInstrument(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getSearchList(page, pageSize, keyword, IConstants.INSTRUMENT)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                    getView().hideLoading();
                    parseSearchInstrument(searchInfo);
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);
                }));
    }

    private void parseInstrumentBean(InstrumentBean instrumentBean) {
        List<InstrumentBean> nationalList = instrumentBean.getNationalList();
        List<InstrumentSection> sectionList = new ArrayList<>();

        for (int i = 0; i < nationalList.size(); i++) {
            InstrumentBean bean = nationalList.get(i);
            sectionList.add(new InstrumentSection(true, bean.getNationalName()));
            List<InstrumentBean.InstrumentListBean> list = bean.getInstrumentList();
            for (InstrumentBean.InstrumentListBean listBean : list) {
                sectionList.add(new InstrumentSection(listBean));
            }
        }

        getView().setInstrumentListUI(sectionList, 1);
    }

    private void parseSearchInstrument(SearchInfo searchInfo) {
        List<InstrumentBean.InstrumentListBean> instrumentList = searchInfo.getInstrumentList();
        List<InstrumentSection> sectionList = new ArrayList<>();
        for (InstrumentBean.InstrumentListBean bean : instrumentList) {
            sectionList.add(new InstrumentSection(bean));
        }

        getView().setInstrumentListUI(sectionList, searchInfo.getPageNum());
    }
}
